package com.ymk.cmd;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ymk.base.cmd.ACmd;
import com.ymk.base.cmd.CmdSign;
import com.ymk.base.exception.MallException;
import com.ymk.base.service.WxService;
import com.ymk.base.utlis.ParamsBuilder;
import com.ymk.bo.user.PayPaymentBO;
import com.ymk.bo.user.UserInfoBO;
import com.ymk.core.code.cmd.UserCmdCode;
import com.ymk.core.constants.WxConstants;
import com.ymk.core.enums.orders.OrdersItemStatusEnum;
import com.ymk.service.IPayPaymentService;
import com.ymk.service.IUserInfoService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Objects;

@Service
public class UserTransferMoneyCmd extends ACmd {

    @Override
    public CmdSign executeMop(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        UserInfoBO reqData = super.getData(sign, UserInfoBO.class);
        String openId = request.getHeader(WxConstants.X_WX_OPENID);

        ParamsBuilder paramsBuilder = ParamsBuilder.newBuild()
                .addParam("userId", reqData.getUserId());
        UserInfoBO userInfoBO = userService.get(paramsBuilder.build());

        paramsBuilder.clearParam().addParam("wxOpenId",openId);
        UserInfoBO manager = userService.get(paramsBuilder.build());
        sign.setUserId(Integer.valueOf(manager.getId()));
        if(Objects.isNull(userInfoBO)){
            throw new MallException("用户不存在，请核实用户id");
        }
        userInfoBO.setAmount(userInfoBO.getAmount().add(reqData.getAmount()));
        this.setModifiedData(sign,userInfoBO);
        this.userService.update(userInfoBO);

        savePayment(sign,reqData);

        return respSign;
    }

    private void savePayment(CmdSign sign,UserInfoBO userInfoBO ) {
        PayPaymentBO payPaymentBO = new PayPaymentBO();
        payPaymentBO.setPayNo("0");
        payPaymentBO.setUserId(userInfoBO.getUserId());
        payPaymentBO.setAmount(userInfoBO.getAmount());
        payPaymentBO.setPayType("2");
        payPaymentBO.setPayDate(new Date());
        payPaymentBO.setRemark("充值");
        payPaymentBO.setStatus(OrdersItemStatusEnum.SUCCESS.getStatus());
        this.setCreatedData(sign,payPaymentBO);
        this.payPaymentService.post(payPaymentBO);
    }

    @Override
    public boolean anon(CmdSign sign) {
        return true;
    }

    @Override
    public CmdSign verify(HttpServletRequest request, CmdSign sign) throws Exception {
        UserInfoBO reqData = super.getData(sign, UserInfoBO.class);
        String openId = request.getHeader(WxConstants.X_WX_OPENID);
        if (StringUtils.isBlank(openId)) {
            throw new MallException("请求异常,header未发现openID");
        }

        if (StringUtils.isBlank(reqData.getUserId())) {
            throw new MallException("用户id不能为空");
        }
        if (Objects.isNull(reqData.getAmount())) {
            throw new MallException("充值金额不能为空");
        }

        return null;
    }

    @Override
    public String getCmdCode() {
        return UserCmdCode.USER_TRANSFER_MONEY;
    }

    @Autowired
    private IPayPaymentService payPaymentService;
    @Autowired
    private WxService wxService;
    @Autowired
    private IUserInfoService userService;
}